package edu.nf.waimai.dao.shop.impl;

import edu.nf.util.DBUtils;
import edu.nf.waimai.dao.shop.ShopDAO;
import edu.nf.waimai.model.shop.Shop;
import edu.nf.waimai.model.shop.ShopType;
import edu.nf.waimai.model.shop.waimai.WaimaiShop;
import org.nf.db.util.SqlExecutor;
import org.nf.db.util.result.BeanHandler;
import org.nf.db.util.result.BeanListHandler;

import java.util.ArrayList;
import java.util.List;

/**
 * 商家 dao 接口的实现类
 */
public class ShopDAOImpl implements ShopDAO {
    /**
     * 返回全部店铺
     *
     * @return
     */
    @Override
    public List<Shop> listShop() {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        BeanListHandler<Shop> listHandler = new BeanListHandler<>(Shop.class);
        String sql = "SELECT shop_id, shop_img, shop_types_id, u_id, shop_name, shop_phone, shop_address, shop_datetime FROM shop";
        return sqlExecutor.executeQuery(sql, listHandler);
    }

    /**
     * 商家注册
     * @param shop
     * @return
     */
    @Override
    public int registerShop(Shop shop) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = "insert into shop(shop_types_id,u_id,shop_name,shop_phone,shop_address,shop_img) values(?,?,?,?,?,?)";
        return sqlExecutor.executeUpdate(sql, shop.getShopTypesId(),shop.getUid() , shop.getShopName(), shop.getShopPhone(), shop.getShopAddress(),shop.getShopImg());
    }

    /**
     * 查询手机号是否注册
     * @param phone
     * @return
     */
    @Override
    public Shop equeryPhone(String phone) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());

        String sql ="select shop_phone from shop where shop_phone = ?";

        BeanHandler<Shop> shopBeanHandler = new BeanHandler<>(Shop.class);

        return sqlExecutor.executeQuery(sql,shopBeanHandler,phone);
    }

    /**
     * 通过名称查找全部店铺
     * @param shopName
     * @return
     */
    @Override
    public List<Shop> listShopByShopName(String shopName) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        BeanListHandler<Shop> listHandler = new BeanListHandler<>(Shop.class);
        String sql = "SELECT shop_id, shop_img, shop_types_id, u_id, shop_name, shop_phone, shop_address, shop_datetime FROM shop WHERE shop_name LIKE ?";
        String param = "%"+shopName+"%";
        return sqlExecutor.executeQuery(sql,listHandler,param);
    }

    /**
     * 查询全部商家分页
     * @param shop
     * @param page
     * @param limit
     * @return
     */
    @Override
    public List<Shop> listShopPaging(Shop shop, int page, int limit) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        //定义一个集合，存储动态sql中的占位符
        List<Object> params = new ArrayList<>();
        //用于sql拼接
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT shop_id, shop_img, shop_types_id,");
        sql.append( "shop_name, shop_phone, shop_address, shop_datetime, u_id ,shop_details FROM shop where 1=1");

        if (shop != null && shop.getShopName() != null && !"".equals(shop.getShopName())){
            sql.append(" and shop_name like ? ");
            System.out.println(shop.getShopName());
            params.add( shop.getShopName() + "%");
        }
        if (shop != null && shop.getShopAddress() != null && !"".equals(shop.getShopAddress())){
            sql.append(" and shop_address like ? ");
            params.add( shop.getShopAddress() + "%");
        }
        sql.append(" limit ?,?");
        params.add((page - 1) * limit);
        params.add(limit);
        BeanListHandler<Shop> beanListHandler = new BeanListHandler<>(Shop.class);
        return sqlExecutor.executeQuery(sql.toString(),beanListHandler,params.toArray());
    }

    @Override
    public List<Shop> listTypeShopPaging(Shop shop, int page, int limit) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());

        //用于sql拼接
        String sql = "SELECT shop_id, shop_img, shop_types_id,shop_name, shop_phone, shop_address, shop_datetime, u_id ,shop_details FROM shop where shop_types_id = ? limit ?,?";
        BeanListHandler<Shop> beanListHandler = new BeanListHandler<>(Shop.class);
        return sqlExecutor.executeQuery(sql,beanListHandler,shop.getShopTypesId(),(page - 1) * limit,limit);
    }

    /**
     * 删除店铺
     * @param shopId
     * @return
     */
    @Override
    public int delShop(int shopId) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = "delete from shop where shop_id = ?";
        return sqlExecutor.executeUpdate(sql,shopId);
    }

    @Override
    public ShopType getShopType(int id) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = "select shop_type_id ,shop_type_name from shop_type where shop_type_id = ?";
        return sqlExecutor.executeQuery(sql,new BeanHandler<>(ShopType.class) , id);
    }

    @Override
    public Shop getShop(int uid) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = "SELECT shop_id, shop_img, shop_types_id, u_id, shop_name, shop_phone, shop_address, shop_datetime FROM shop WHERE u_id = ?";
        return sqlExecutor.executeQuery(sql,new BeanHandler<>(Shop.class),uid);
    }


}
